using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Soyuz5.Utility
{
    public static class IO
    {
        /// <summary>
        /// Creates a deep directory
        /// </summary>
        /// <param name="Path"></param>
        public static void CreateDirectory(string path)
        {
            if (!Directory.Exists(path))
            {
                string[] strPathParts = path.Split(System.IO.Path.DirectorySeparatorChar);
                string strPath = string.Empty;
                bool isUNC = path.StartsWith(@"\\");

                for (int i = 0; i < strPathParts.Length; i++)
                {
                    strPath += strPathParts[i] + System.IO.Path.DirectorySeparatorChar;
                    if ((!isUNC || i > 2) && !Directory.Exists(strPath))
                    {
                        Directory.CreateDirectory(strPath);
                    }
                }
            }
        }

        /// <summary>
        /// Calculates the SHA256 hash of a given file, and returns the results as an base64 encoded string
        /// </summary>
        /// <param name="path">The path to the file to calculate the hash for</param>
        /// <returns>The base64 encoded SHA256 hash</returns>
        public static string CalculateHash(string path)
        {
            System.Security.Cryptography.HashAlgorithm sha = System.Security.Cryptography.HashAlgorithm.Create("SHA256");
            using (System.IO.FileStream fs = System.IO.File.Open(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read))
                return Convert.ToBase64String(sha.ComputeHash(fs));
        }
    }
}
